//
//  CAReplicatorLayerViewController.m
//  CoreAnimation
//
//  Created by 张国文 on 16/10/17.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "CAReplicatorLayerViewController.h"
#import "UIDevice+XJDevice.h"

@interface CAReplicatorLayerViewController ()

@property (nonatomic, strong) UIView *backgroundView;

@end

@implementation CAReplicatorLayerViewController

/**
 *
 http://www.jianshu.com/p/84455b674f55 这个链接上讲的很好
 *基本属性，属性说明
 instanceCount
 var instanceCount: Int
 拷贝图层的次数,包括其所有的子图层,默认值是1,也就是没有任何子图层被复制
 
 instanceDelay
 var instanceDelay: CFTimeInterval
 在短时间内的复制延时,一般用在动画上(支持动画的延时)
 
 instanceTransform
 var instanceTransform: CATransform3D
 复制图层在被创建时产生的和上一个复制图层的位移(位移的锚点时CAReplicatorlayer的中心点)
 
 preservesDepth
 var preservesDepth: Bool
 如果设置为YES,图层将保持于CATransformLayer类似的性质和相同的限制
 
 instanceColor
 var instanceColor: CGColor?
 设置多个复制图层的颜色,默认位白色
 
 instanceRedOffset
 var instanceRedOffset: Float
 设置每个复制图层相对上一个复制图层的红色偏移量
 
 instanceGreenOffset
 var instanceGreenOffset: Float
 设置每个复制图层相对上一个复制图层的绿色偏移量
 
 instanceBlueOffset
 var instanceBlueOffset: Float
 设置每个复制图层相对上一个复制图层的蓝色偏移量
 
 instanceAlphaOffset
 var instanceAlphaOffset: Float
 设置每个复制图层相对上一个复制图层的透明度偏移量
 
 
 文／SSBun（简书作者）
 原文链接：http://www.jianshu.com/p/84455b674f55
 著作权归作者所有，转载请联系作者获得授权，并标注“简书作者”。
 */

- (BOOL)shouldAutorotate
{
    return NO;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeLeft;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [UIDevice setOrientation:UIInterfaceOrientationPortrait];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self createView];

}

- (void)createView
{
    self.backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.height, self.view.frame.size.width)];
    self.backgroundView.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.5];
    [self.view addSubview:_backgroundView];
    
    UIButton *returnButton = [UIButton buttonWithType:UIButtonTypeCustom];
    returnButton.frame = CGRectMake(10, 10, 100, 30);
    [returnButton setTitle:@"返回" forState:UIControlStateNormal];
    returnButton.backgroundColor = [UIColor cyanColor];
    [returnButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [returnButton addTarget:self action:@selector(returnBack:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:returnButton];
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    button1.frame = CGRectMake(120, 10, 160, 30);
    [button1 setTitle:@"创建简单复制图层" forState:UIControlStateNormal];
    button1.backgroundColor = [UIColor yellowColor];
    [button1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(createCAReplicator:) forControlEvents:UIControlEventTouchUpInside];
    button1.tag = 100;
    [self.view addSubview:button1];
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    button2.frame = CGRectMake(290, 10, 150, 30);
    [button2 setTitle:@"创建音乐浮动" forState:UIControlStateNormal];
    button2.backgroundColor = [UIColor yellowColor];
    [button2 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(createCAReplicator:) forControlEvents:UIControlEventTouchUpInside];
    button2.tag = 101;
    [self.view addSubview:button2];
    
    UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];
    button3.frame = CGRectMake(450, 10, 150, 30);
    [button3 setTitle:@"创建加载动画" forState:UIControlStateNormal];
    button3.backgroundColor = [UIColor yellowColor];
    [button3 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [button3 addTarget:self action:@selector(createCAReplicator:) forControlEvents:UIControlEventTouchUpInside];
    button3.tag = 102;
    [self.view addSubview:button3];
    
    UIButton *button4 = [UIButton buttonWithType:UIButtonTypeCustom];
    button4.frame = CGRectMake(120, 50, 180, 30);
    [button4 setTitle:@"大小变化的加载动画" forState:UIControlStateNormal];
    button4.backgroundColor = [UIColor yellowColor];
    [button4 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [button4 addTarget:self action:@selector(createCAReplicator:) forControlEvents:UIControlEventTouchUpInside];
    button4.tag = 103;
    [self.view addSubview:button4];
    
    UIButton *button5 = [UIButton buttonWithType:UIButtonTypeCustom];
    button5.frame = CGRectMake(310, 50, 180, 30);
    [button5 setTitle:@"反射" forState:UIControlStateNormal];
    button5.backgroundColor = [UIColor yellowColor];
    [button5 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [button5 addTarget:self action:@selector(createCAReplicator:) forControlEvents:UIControlEventTouchUpInside];
    button5.tag = 104;
    [self.view addSubview:button5];
}

- (void)returnBack:(UIButton *)button
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)createCAReplicator:(UIButton *)button
{
    for (int i = 0; i < self.backgroundView.layer.sublayers.count; i++) {
        if ([self.backgroundView.layer.sublayers[i] isMemberOfClass:[CALayer class]]) {
            [self.backgroundView.layer.sublayers[i] removeFromSuperlayer];
        }
    }
  
    switch (button.tag) {
        case 100:
        {
            // 创建简单的复制图层
            [self createSimpleCAReplicatorLayer];
            break;
        }
            case 101:
        {
            // 根据CAReplicator创建一个音乐条浮动
            [self createMusicUpSideDown];
            break;
        }
            case 102:
        {
            // 用CAReplication创建加载动画
            [self createLoadingAnimation];
            break;
        }
            case 103:
        {
            // // 用CAReplicatorLayer创建由大变小的加载动画
            [self createScaleChangeAnimation];
            break;
        }
            case 104:
        {
            // 反射
            [self reflection];
            break;
        }
        default:
            break;
    }
    for (int i = 0; i < self.view.subviews.count; i++) {
        if ([self.view.subviews[i] isKindOfClass:[UIButton class]]) {
            [self.view bringSubviewToFront:self.view.subviews[i]];
        }
    }
}

// 创建简单的复制图层
- (void)createSimpleCAReplicatorLayer
{
  
    CAReplicatorLayer *replicator = [[CAReplicatorLayer alloc] init];
    replicator.frame = self.backgroundView.bounds;
    [self.backgroundView.layer addSublayer:replicator];
    
    // 添加基础图层，所有的复制图层都是以这一图层为基础变化的
    CALayer *baseLayer = [CALayer layer];
    baseLayer.backgroundColor = [UIColor whiteColor].CGColor;
    baseLayer.bounds = CGRectMake(0, 0, 50, 50);
    baseLayer.position = CGPointMake(self.backgroundView.center.x, 115);
    [replicator addSublayer:baseLayer];
    
    // 创建transform3D变化
    CATransform3D transform3D = CATransform3DIdentity;
    transform3D = CATransform3DTranslate(transform3D, 0, 40, 0);
    transform3D = CATransform3DRotate(transform3D, M_PI / 5, 0, 0, 1);
    transform3D = CATransform3DTranslate(transform3D, 0, -40, 0); // 沿着斜角Y轴向上平移60
    replicator.instanceTransform = transform3D;
    
    // 设置复制图层的数量，这里我们设置为10个，这样正好是一个圈
    replicator.instanceCount = 10;
    
    // 设置颜色偏移量
    replicator.instanceRedOffset = -0.05;
    replicator.instanceColor = [UIColor redColor].CGColor; // 如果用instanceColor设置图层默认的颜色，则baseLayer的颜色必须为白色，否则为黑色。(如果不用instanceColor设置颜色，则baseLayer的颜色就是图层的默认颜色。)
    
}

// 根据CAReplicator创建一个音乐条浮动
- (void)createMusicUpSideDown
{
    CAReplicatorLayer *replicator = [CAReplicatorLayer layer];
    replicator.frame = self.backgroundView.bounds;
    [self.backgroundView.layer addSublayer:replicator];
    
    // 创建基础图层
    CALayer *baseLayer = [[CALayer alloc] init];
    baseLayer.bounds = CGRectMake(0, 0, 30, 200);
    baseLayer.position = CGPointMake(50, self.backgroundView.bounds.size.height - 30);
    baseLayer.anchorPoint = CGPointMake(0.5, 1);
    baseLayer.backgroundColor = [UIColor cyanColor].CGColor;
    [replicator addSublayer:baseLayer];
    
    // 为每一个复制的子图层创建动画
    CABasicAnimation *baseAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale.y"];
    baseAnimation.fromValue = @1.0;
    baseAnimation.toValue = @0.0;
    baseAnimation.autoreverses = YES;
    baseAnimation.duration = 1.0f;
    baseAnimation.repeatCount = MAXFLOAT;
    [baseLayer addAnimation:baseAnimation forKey:@"changeTransformScaleY"];
    
    // 创建transorm3D
    CATransform3D transform3D = CATransform3DIdentity;
    transform3D = CATransform3DTranslate(transform3D, 50, 0, 0);
    replicator.instanceTransform = transform3D;
    
    replicator.instanceCount = 14;
    replicator.instanceBlueOffset = -0.03;
    replicator.instanceRedOffset = -0.03;
    replicator.instanceGreenOffset = -0.03;
    replicator.instanceDelay = 0.3f;
    
    
    
}

// 用CAReplication创建加载动画
- (void)createLoadingAnimation
{
    [UIColor colorWithRed:0 / 255.0 green: 0 / 255.0 blue:255.0 / 255.0 alpha:1];
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake((self.backgroundView.bounds.size.width - 200) / 2, (self.backgroundView.bounds.size.height - 200) / 2 + 30, 200, 200)];
 //   UIBezierPath *bezierPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.backgroundView.center.x, self.backgroundView.center.y + 30) radius:100 startAngle:0 endAngle:2 * M_PI clockwise:YES];
    CAShapeLayer *baseLayer = [[CAShapeLayer alloc] init];
    baseLayer.backgroundColor = [UIColor redColor].CGColor;
    baseLayer.bounds = CGRectMake(0, 0, 20, 20);
    baseLayer.cornerRadius = 10;
    
    // 为baseLayer创建动画
    CAKeyframeAnimation *keyAnimaton = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    keyAnimaton.path = bezierPath.CGPath;
    keyAnimaton.duration = 4.0f;
    keyAnimaton.repeatCount = MAXFLOAT;
    [baseLayer addAnimation:keyAnimaton forKey:@"changePosition"];
    
    
    CAReplicatorLayer *replicator = [CAReplicatorLayer layer];
    replicator.frame = self.backgroundView.frame;
    [replicator addSublayer:baseLayer];
    replicator.instanceCount = 20;
    replicator.instanceDelay = 0.2f;
    replicator.instanceAlphaOffset = -0.05;
    [self.backgroundView.layer addSublayer:replicator];
    

    
}

// 用CAReplicatorLayer创建由大变小的加载动画
- (void)createScaleChangeAnimation
{
    CAShapeLayer *baseLayer = [[CAShapeLayer alloc] init];
    baseLayer.backgroundColor = [UIColor redColor].CGColor;
    baseLayer.bounds = CGRectMake(0, 0, 20, 20);
    baseLayer.position = CGPointMake(10, 135);
    baseLayer.cornerRadius = 10;
    
    CABasicAnimation *baseAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    baseAnimation.fromValue = @1;
    baseAnimation.toValue = @0;
    baseAnimation.duration = 4.0f;
    baseAnimation.repeatCount = MAXFLOAT;
    [baseLayer addAnimation:baseAnimation forKey:@"changeTransformScale"];
    
    CAReplicatorLayer *replicator = [[CAReplicatorLayer alloc] init];
    replicator.frame = CGRectMake(200, 100, 270, 270);
    CGFloat angle = 2 * M_PI / 20;
    replicator.instanceAlphaOffset = -0.05;
    replicator.instanceCount = 20;
    replicator.instanceDelay = 0.2f;
    replicator.instanceTransform = CATransform3DMakeRotation(angle, 0, 0, 1);
    [replicator addSublayer:baseLayer];
    [self.backgroundView.layer addSublayer:replicator];
    
}

// 反射
- (void)reflection
{
    UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    imageV.image = [UIImage imageWithName:@"snowMan"];
    
    CALayer *baseLayer = imageV.layer;
    baseLayer.bounds = CGRectMake(0, 0, 100, 100);
    baseLayer.position = CGPointMake(self.backgroundView.center.x, 50);
    
    CATransform3D transform3D = CATransform3DIdentity;
    transform3D = CATransform3DTranslate(transform3D, 0, 100 + 2, 0);
    transform3D = CATransform3DScale(transform3D, 1, -1, 0);

    CAReplicatorLayer *replicator = [[CAReplicatorLayer alloc] init];
    replicator.frame = CGRectMake(0, 100, self.backgroundView.frame.size.width, 100); // CAReplicator的高度必须和图片的高度一样，否则会出现错误
    [replicator addSublayer:baseLayer];
    replicator.instanceTransform = transform3D;
    replicator.instanceCount = 2;
    replicator.instanceAlphaOffset = -0.6f;
    [self.backgroundView.layer addSublayer:replicator];
}

@end
